/**
 * 
 */
package com.wubo.sec.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wubo.common.util.Assert;
import com.wubo.common.util.ObjectUtils;
import com.wubo.sec.SecurityManager;
import com.wubo.sec.config.LangUtil;
import com.wubo.sec.event.EntityEvent;
import com.wubo.sec.event.RoleEvent;
import com.wubo.sec.log.LogHelper;
import com.wubo.sec.model.Role;
import com.wubo.sec.service.IRoleService;
import com.wubo.sec.util.ObjectDifferenceChecker;
import com.wubo.sec.util.UUID;
import com.wubo.spring.hibernate.BaseHibernateDao;

/**
 * @author wubo
 * @CreateDate 2010-8-3
 * @version 1.0.01
 */
@Service
@SuppressWarnings("unchecked")
public class RoleService implements IRoleService {
	
	@Autowired
	private BaseHibernateDao dao;
	
	public BaseHibernateDao getDao() {
		return dao;
	}
	
	public void setDao(BaseHibernateDao dao) {
		this.dao = dao;
	}

	public String create(Role role) {
		Assert.notNull(role);
		Assert.hasText(role.getRoleName());
		
		role.setCode(UUID.uuid());
		
		java.util.Date now = new java.util.Date();
		String username = SecurityManager.getCurrentUser().getUsername();
		role.setCreateTime(now);
		role.setCreateUser(username);
		role.setUpdateTime(now);
		role.setUpdateUser(username);
		
		onAdd(role);
		
		String code = (String) dao.save(role);
		
		afterAdd(role);
		
		return code;
	}

	public void delete(Role role) {
		Assert.notNull(role);
		
		onDelete(role);
		
		dao.delete(role);
		
		afterDelete(role);
	}

	public List<Role> findAll() {
		dao.setCacheQueries(true);
		return dao.find("from Role r");
	}
	
	public List<Role> find(String filter, Object[] params) {
		return dao.find("select r from Role r "+filter, params);
	}
	
	public List<Role> findRunAs(String filter, Object[] params) {
		return find(filter, params);
	}

	public List<Role> getSplitPage(int start, int limit, String where, Object... params) {
		return dao.queryForPage(start, limit, "select r from Role r "+where, params);
	}
	
	public long getTotal(String where, Object... params) {
		Long result = dao.queryForObject("select count(r.code) from Role r "+where, params);
		return result;
	}

	public Role update(Role role) {
		Assert.notNull(role);
		Assert.hasText(role.getCode());
		Assert.hasText(role.getRoleName());
		
		Role temp = get(role.getCode());
		
		Role old = temp.clone();
		
		temp.setRoleName(role.getRoleName());
		temp.setDesn(role.getDesn());
		temp.setLocked(role.isLocked());
		java.util.Date now = new java.util.Date();
		String username = SecurityManager.getCurrentUser().getUsername();
		temp.setUpdateTime(now);
		temp.setUpdateUser(username);
		
		onUpdate(temp);
		
		dao.update(temp);
		
		afterUpdate(old, temp);
		
		return temp;
	}
	
	public Role get(String code) {
		return dao.get(Role.class, code);
	}
	
	public List<String> listName(String name) {
		return dao.find("select r.roleName from Role r where r.roleName like '%"+name+"%'");
	}
	
	/*----- 发布事件  -----*/
	protected void onAdd(Role role){
		SecurityManager.publishEvent(new RoleEvent(role, EntityEvent.ADD));
	}
	protected void onUpdate(Role role){
		SecurityManager.publishEvent(new RoleEvent(role, EntityEvent.UPDATE));
	}
	protected void onDelete(Role role){
		SecurityManager.publishEvent(new RoleEvent(role, EntityEvent.DELETE));
	}
	/*----- 记录日志 -----*/
	protected void afterAdd(Role role){
		LogHelper.actionLog(LangUtil.get("CreateRole"), 
				LangUtil.get("RoleDetail", ObjectUtils.objToString(role)));
		
	}
	protected void afterUpdate(Role old, Role role){
		LogHelper.actionLog(LangUtil.get("UpdateRole"), 
				LangUtil.get("UpdateRoleDetail", ObjectDifferenceChecker.check(old, role)));
		
	}
	protected void afterDelete(Role role){
		LogHelper.actionLog(LangUtil.get("DeleteRole"), 
				LangUtil.get("RoleDetail", ObjectUtils.objToString(role)));
	}
}
